package com.gistmap.web;

import com.gistmap.bean.Activity;
import com.gistmap.bean.User;
import com.gistmap.dto.ActivityDto;
import com.gistmap.service.ActivityService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Administrator on 2017/4/17.
 */
@Controller
@RequestMapping("/act")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(Activity activity, HttpServletRequest request, Model model)
    {
        User user = (User)request.getSession().getAttribute("user");
        activity.setUid(user.getId());

        Integer actId = activityService.save(activity);
        Map<String,Object> param = new HashedMap();

        param.put("act_id",activity.getId());
        param.put("uid",user.getId());

        activityService.joinSelf(param);
        model.addAttribute("tip","5");
        return "tips";
    }

    /*
        参加活动(加入t_act_user表-活动表当前人数+1)
     */
    @RequestMapping(value = "/join",method = RequestMethod.POST)
    public void join(int actId, int actUid, String actTitle, HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User)request.getSession().getAttribute("user");
        int uid = user.getId();
        String result = "";


        Map<String,Object> param = new HashedMap();
        param.put("act_id",actId);
        param.put("uid",uid);
        Map<String,Object> paramForRecord = new HashedMap();
        paramForRecord.put("oid",uid);
        paramForRecord.put("bid",actUid);
        paramForRecord.put("opt",5);
        paramForRecord.put("source",actTitle);

        try {
            activityService.join(param,paramForRecord);
            result = "1";
        }
        catch (Exception e)
        {
            result="0";
            e.printStackTrace();
        }

        response.getWriter().write(result);
    }

    @RequestMapping(value = "/joinInfo",method = RequestMethod.POST)
    public void jonInfo(int uid,HttpServletResponse response) throws IOException {
        List<Integer> joinList = activityService.getJoinInfo(uid);
        JSONArray jsonArray = JSONArray.fromObject(joinList);
        String result = jsonArray.toString();
        response.getWriter().write(result);
    }

    @RequestMapping("/showMyAct")
    public ModelAndView showAct(Integer uid)
    {
        ModelAndView mv = new ModelAndView("myAct");
        List<ActivityDto> ijoin = activityService.getJoinAct(uid);

        mv.addObject("ijoin",ijoin);
        return mv;
    }

    @RequestMapping("/allActivitys")
    public ModelAndView getAllActivity()
    {
        ModelAndView mv = new ModelAndView("allActs");
        List<ActivityDto> list = activityService.allActs();
        mv.addObject("ActShow",list);
        return mv;
    }


}
